package com.mach.platform.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
import com.mach.platform.domain.Response;
import com.mach.platform.domain.mach.QuotaRecord;
import com.mach.platform.service.QuotaRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.controller
 * @time 2018/6/5 0005 16:11
 */
@Api(tags = { "后台配置---数据" })
@RestController
@RequestMapping(value = "$quota")
public class QuotaRecordCtrl  extends BaseCtrl<QuotaRecord>{

	@Autowired
	private QuotaRecordService quotaRecordService;

	/**
	 * -- @GetMapping(value = "/info/{id}")
	 * -- @ApiOperation(value = "单个实体信息", notes = "接口备注：无", produces = "application/json")
	 * 单个数据 的接口
	 * -- @PathVariable(value = "id", required = true) Long id
	 *
	 * @param id 数据的id
	 * @return Response
	 */
	@GetMapping(value = "/info/{id}")
	@ApiOperation(value = "单个实体信息", notes = "接口备注：无", produces = "application/json")
	@Override
	public Response info (@PathVariable(value = "id", required = true) Long id) {

		QuotaRecord dbResult = quotaRecordService.info(id);

		return Response.ok(dbResult);
	}

	/**
	 * --	@ApiOperation(value = "列表--分页", produces = "application/json", notes = "列表参数：size(每页数量),page(页码),searchParam(查询内容)")
	 * -- @GetMapping(value = "/list")
	 * 获取列表数据
	 * -- @PageableDefault(size = 25) Pageable pageable
	 * -- @RequestParam Map params
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Response
	 */
	@ApiOperation(value = "列表--分页", produces = "application/json", notes = "列表参数(非 json 发送，拼接发送)：size(每页数量),page(页码), factory_createUser_baseCode.name_plike(查询内容,默认传空则不搜索该字段)")
	@GetMapping(value = "/list")
	@Override
	public Response list (@PageableDefault(size = 25) Pageable pageable, @RequestParam Map params) {

		Page<QuotaRecord> dbResult = quotaRecordService.list(pageable, params);

		return Response.ok(dbResult);

	}

	/**
	 * -- @DeleteMapping("/del")
	 * -- @ApiOperation(value = "删除", notes = "接口备注：无", produces = "application/json")
	 * 删除数据
	 * -- @RequestBody Long[] ids
	 *
	 * @param ids id的集合
	 * @return Response
	 */
	@DeleteMapping("/del")
	@ApiOperation(value = "删除", notes = "接口备注：接收数组，json发送", produces = "application/json")
	@Override
	public Response delete (@RequestBody Long[] ids) {

		int delete = quotaRecordService.delete(ids);

		return Response.ok(delete);
	}

	/**
	 * --@PostMapping("/save")
	 * --	@ApiOperation(value = "保存", notes = "接口备注：无", produces = "application/json")
	 * 保存一个 对象
	 * --@RequestBody T t
	 *
	 * @param quotaRecord 保存的实体
	 * @return Response
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增保存", notes = "接口备注：无", produces = "application/json")
	@Override
	public Response save (@RequestBody QuotaRecord quotaRecord) {

		QuotaRecord dbResult = quotaRecordService.save(quotaRecord);

		return Response.ok(dbResult);
	}

	/**
	 * --@PutMapping("/upd")
	 * --	@ApiOperation(value = "更新", notes = "接口备注：无", produces = "application/json")
	 * 更新一个对象
	 * --@RequestBody T t
	 *
	 * @param quotaRecord 更新的实体
	 * @return Response
	 */
	@PutMapping("/upd")
	@ApiOperation(value = "编辑保存", notes = "接口备注：原对象json返回", produces = "application/json")
	@Override
	public Response update (@RequestBody QuotaRecord quotaRecord) {

		QuotaRecord dbResult = quotaRecordService.update(quotaRecord);

		return Response.ok(dbResult);
	}

	/**
	 * 导入 excel 数据
	 *
	 * @param file
	 * @return
	 */
	@PostMapping("import")
	public Response importDevicesDatas (@RequestParam("file") MultipartFile file) {

		if (file == null) {
			return Response.error("上传文件不能为空！");
		}

		List<QuotaRecord> result = null;
		int importDatas = 0;

		ImportParams importParams = new ImportParams();
		importParams.setTitleRows(0);

		try {
			result = ExcelImportUtil.importExcel(file.getInputStream(), QuotaRecord.class, importParams);
			importDatas = quotaRecordService.importDatas(result);
		} catch (Exception e) {
			if (e.getCause() instanceof ExcelImportException) {
				return Response.error("转换中，数据类型出错，请检查该填数字的地方不能填中文：" + e.getMessage());
			}
		}
		return Response.ok(importDatas);
	}

}
